package com.mj.text;

import com.mj.Vo;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author：作者姓名
 * @Package：com.mj.text
 * @Project：g6_jdk1.8tz
 * @name：LambdaSned
 * @Date：2024/5/22 9:45
 */
@SpringBootTest
public class LambdaSned {
    @Test
    public void lambda1(){
        List<String> girls = Arrays.asList("小美", "圆圆", "黄小丫");

        for (String girl : girls) {
            System.out.println(girl);
        }

      //Labda表达式
        girls.forEach(girl-> System.out.println(girl));


    }
    //传统排列
    @Test
    public void lambda2(){
        List<String> scores = Arrays.asList("45", "98", "86");

        //传统排序
        Collections.sort(scores, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println("------------------------------------");

        //lambda表达式排序
        Collections.sort(scores);
//
        scores.forEach(scpre -> System.out.println(scpre));




    }
    @Test
    public void lambda3(){
        List<String> fruits = Arrays.asList("apple", "banana", "pair");
        ArrayList<String> fruitFruits = new ArrayList<>();


      //传统过滤
        for (String fruit : fruits) {
            if (fruit.startsWith("a")){
                fruitFruits.add(fruit);
            }
        }



        //lambda表达过滤
        List<String> fruitFruit = fruits.stream()
                .filter(fruit -> fruit.startsWith("a"))
                .collect(Collectors.toList());

        System.out.println(fruitFruit);

    }



    //进行map的映射(lambda)
    @Test
    public void  lambda4(){
        List<String> fruits = Arrays.asList("apple", "banana", "pair");
        ArrayList<Integer> lengths = new ArrayList<>();

        //传统
        for (String fruit : fruits) {
            lengths.add(fruit.length());
        }
        lengths.forEach(System.out::println);
        System.out.println("------------------------");


        System.out.println("------------------------");
        //lambda
        List<Integer> length = fruits.stream()
                .map(fruit -> fruit.length())
                .collect(Collectors.toList());

      length.forEach(System.out::println);
    }

    @Test
    public void  lambda5(){
        //普通求和
        List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
        int sum = 0;
        for (Integer num : nums) {
            sum+=num;
        }
        System.out.println(sum);
        System.out.println("------------------------");
        //lambda相加
        int num = nums.stream().mapToInt(Integer::intValue).reduce(0, (a, b) -> a + b);
        System.out.println(num);


    }


    /**
     * 用lambda进行分组
     */
    @Test
    public void lambda7(){
        List<String> fruits = Arrays.asList("aaaa", "bbbb", "aa", "bb");

        Map<Integer, List<String>> groupedFruits = new HashMap<>();
        //基础用法
        for (String fruit : fruits) {
            int length = fruit.length();
            if (!groupedFruits.containsKey(length)){
                groupedFruits.put(length,new ArrayList<>());
            }
            groupedFruits.get(length).add(fruit);
        }



        //Lambda表达式
        groupedFruits = fruits.stream().collect(Collectors.groupingBy(fruit -> fruit.length()));
        System.out.println(groupedFruits);



    }





    /**
     * 根据月份分组，在根据日期分组
     */
    @Test
    public void  lambda6(){
     List<Vo> list = Arrays.asList(
             new Vo(LocalDate.of(2023,1,3),"1"),
             new Vo(LocalDate.of(2023,1,3),"1"),
             new Vo(LocalDate.of(2023,2,1),"1"),
             new Vo(LocalDate.of(2023,2,3),"1"),
             new Vo(LocalDate.of(2023,2,3),"2")
     );
        Map<String, Map<String, List<Vo>>> grouped = list.stream()
                .collect(Collectors.groupingBy(vo -> vo.getLocalDate().getYear()
                                + "-" + vo.getLocalDate().getMonthValue(),
                        Collectors.groupingBy(vo -> vo.getLocalDate().getMonthValue() + "-"
                                + vo.getLocalDate().getDayOfMonth())));


        System.out.println(grouped);

        System.out.println("---------------------------------------------");
        grouped.forEach((yearMonthKey, monthDayMap) -> {
            System.out.println("Year-Month: " + yearMonthKey);
            monthDayMap.forEach((monthDayKey, vos) -> {
                System.out.println("Month-Day: " + monthDayKey + " -> " + vos);
            });
        });

//        System.out.println(grouped);

    }








}
